package edu.neu.swc.cloud.service.impl;

import edu.neu.swc.cloud.dao.ConfigProDao;
import edu.neu.swc.cloud.dao.EquipmentDao;
import edu.neu.swc.cloud.dao.FactoryDao;
import edu.neu.swc.cloud.dao.ProductDao;
import edu.neu.swc.cloud.dao.RentDao;
import edu.neu.swc.cloud.domain.ConfigPro;
import edu.neu.swc.cloud.domain.Equipment;
import edu.neu.swc.cloud.domain.Factory;
import edu.neu.swc.cloud.domain.Product;
import edu.neu.swc.cloud.domain.Rent;
import edu.neu.swc.cloud.service.EquipmentService;
import edu.neu.swc.cloud.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("equipmentService")
public class EquipmentServiceImpl implements EquipmentService {

    @Autowired
    EquipmentDao equipmentDao;

    @Autowired
    RentDao rentDao;

    @Autowired
    ConfigProDao configProDao;

    @Autowired
    ProductDao productDao;

    @Autowired
    FactoryDao factoryDao;

    @Override
    public void addEquip(Equipment equipment) {
        equipment.setEq_code(UuidUtil.getEquCode());
        equipmentDao.addEquipment(equipment);
    }

    @Override
    public void deleteEquipById(int equ_id) {
        equipmentDao.deleteEquipmentById(equ_id);
    }

    @Override
    public void update(Equipment equipment) {
        equipmentDao.updateEquipment(equipment);
    }

    @Override
    public List<Equipment> findAll() {
        return equipmentDao.findAll();
    }

    @Override
    public List<Equipment> findEquipByCon(Equipment equipment) {
        return equipmentDao.findEquByCon(equipment);
    }

    @Override
    public void shutById(int equ_id) {
        equipmentDao.shutDownById(equ_id);
    }

    @Override
    public void startById(int equ_id) {
        equipmentDao.startUpById(equ_id);
    }

    @Override
    public Equipment findEquipById(int equ_id) {
        return equipmentDao.findEquById(equ_id);
    }

    @Override
    public List<Rent> findRentByFacId(int my_fac_id) {
        List<Rent> rentList = rentDao.findRentByFacId(my_fac_id);
        for(Rent rent : rentList){
            Equipment equipment = equipmentDao.findEquById(rent.getRent_equ_id());
            rent.setEquipment(equipment);
        }
        return rentList;
    }

    @Override
    public List<Equipment> findEquipAvail() {
        return equipmentDao.findEquipAvail();
    }

    @Override
    public void rentEquip(Rent rent) {
        rentDao.addRent(rent);
        rentDao.addRentHistory(rent);
        equipmentDao.setRentById(rent);
    }

    @Override
    public void returnBack(int equ_id) {
        rentDao.deleteRentByEquId(equ_id);
        equipmentDao.setBackById(equ_id);
    }

    @Override
    public List<ConfigPro> findConfigByEquId(int eq_id) {
        List<ConfigPro> configProList = configProDao.findCPByEquId(eq_id);
        for(ConfigPro configPro : configProList){
            Product product = productDao.findProById(configPro.getEp_pro_id());
            Equipment equipment = equipmentDao.findEquById(configPro.getEp_equ_id());
            configPro.setEp_equip(equipment);
            configPro.setEp_product(product);
        }
        return configProList;
    }

    @Override
    public void AddConfig(ConfigPro configPro) {
        List<ConfigPro> configProList = configProDao.isConfig(configPro);
        int isConfig = configProList.size();
        System.out.println("config:" + isConfig);
        System.out.println(configPro);
        if(isConfig == 0){
            configProDao.addConfig(configPro);
        }else{
            configProDao.updateConfig(configPro);
        }
    }

    @Override
    public List<Rent> findRentByFacIdAndCon(int my_fac_id, Equipment equipment) {
        List<Equipment> con_equ = equipmentDao.findEquByCon(equipment);
        List<Rent> con_rent = rentDao.findRentByFacId(my_fac_id);
        for(Rent rent : con_rent){
            Equipment equip = equipmentDao.findEquById(rent.getRent_equ_id());
            rent.setEquipment(equip);
        }
        return con_rent;
    }

    @Override
    public List<Rent> findRentByEquId(int eq_id) {
        List<Rent> record = rentDao.findRecordByEquId(eq_id);
        for(Rent rent : record){
            Factory factory = factoryDao.findFacById(rent.getRent_fac_id());
            rent.setFactory(factory);
        }
        return record;
    }

    @Override
    public int getEquipCounts(String eq_name) {
        return equipmentDao.getEquipCounts(eq_name);
    }

    @Override
    public List<Equipment> findEquipByQuery(String eq_name, int pageStart, int pageSize) {
        return equipmentDao.findEquipByQuery(eq_name,pageStart,pageSize);
    }

    @Override
    public int getMyEquipCounts(String eq_name, int fac_id) {
        return equipmentDao.getMyEquipCounts(eq_name,fac_id);
    }

    @Override
    public List<Equipment> findMyEquipByQuery(String eq_name, int fac_id, int pageStart, int pageSize) {
        return equipmentDao.findMyEquipByQuery(eq_name,fac_id,pageStart,pageSize);
    }

    @Override
    public int getConfigCounts(String s, int fac, int equ) {
        return configProDao.getConfigCounts(fac,equ);
    }

    @Override
    public List<ConfigPro> findConfigByQuery(String s, int fac, int equ, int pageStart, int pageSize) {
        List<ConfigPro> configProList = configProDao.findConfigByQuery(fac,equ,pageStart,pageSize);
        for(ConfigPro configPro : configProList){
            Product product = productDao.findProById(configPro.getEp_pro_id());
            configPro.setEp_pro_name(product.getPro_name());
            configPro.setEp_pro_code(product.getPro_code());
        }
        return configProList;
    }

    @Override
    public List<Equipment> findEquipByFacId(int id) {
        return equipmentDao.findEquByFacId(id);
    }

    @Override
    public void addFacEquip(Equipment equipment) {
        equipment.setEq_rent(-1);
        String next_code = UuidUtil.getEquCode();
        equipment.setEq_code(next_code);
        //将设备插入设备表
        equipmentDao.addEquipment(equipment);
        //获取加入设备的ID
        Equipment newEquip = equipmentDao.findEquByCode(next_code);
        //将设备拥有关系插入租用表
        equipmentDao.addOwnRel(newEquip.getEq_id(),newEquip.getEq_fac_id());
    }
}
